/*****************************************************
PROJECT: BRIDGE C2 SCRAP
JOB:
REQUESTOR:Rachel Gold
JIRA #:
ANALYST: Jean O'Malley
DATE: 8/4/2022
7/23/2023 corrected coding for orders within 1 year of due
corrected filtering for patients with 1 year observation after first due date
9/12/2023 limit orders to documented orders and completions to completions of documented orders
VERSION: 
LAST UPDATED: 
PEER REVIEWER: 

PROJECT DESCRIPTION: Use ASCEND approach to compare CRC, CVC and 
BC cancer prevention services in patients with
	No SDH Screening
	Documented Need
	Documented No Need
Stratified by SDH need: Food, Housing, Transportation
STUDY PERIOD: 7/1/2016-2/29/2020		

DETAILS (this file): 

Table 2.Columns Food Insecurity: Need/ No Need/ Not documented
Table 3 Columns Housing Insecurity: Need/ No Need/ Not documented
Table 4 Columns Transportation Insecurity: Need/ No Need/ Not documented
		

INPUT DATA:
SCRAP.CVC_outcomes;

OUTPUT DATA: corrected excel file SCRAP CVC Cohort tables 09112023


SECTIONS:
A. Dynamic variables and analysis file

C.Among Patients due 12 months before obs end > filtering on this section corrected 
	C1.Receipt of any documented PAP order within 12 months

D.Among patients with order, 
	D1. receipt of screen while due(Poisson with months after due as offset)


****************************************************/
options compress=yes reuse=yes spool;
libname SCRAP "e:\\sasroot\BRIDGE C2\SCRAP\";
libname RD odbc noprompt="dsn=research_dev;Trusted_Connection=yes" bulkload=yes;
options mprint symbolgen;
/****************************************************/
/***************************************************
A. Dynamic variables and analysis file
***************************************************/
%Let covariates=female race_ethnic2 language_c  FPL_cat2 age_cat payor_type_research visit_cat;
%Let class=delivery_site_id female age_cat race_ethnic2 language_c  FPL_cat2 visit_cat payor_type_research;




data analysis;
set SCRAP.CVC_analysis_09122023;
run;
proc freq data=analysis;
tables order_1y_corrected*documented_pap_1yr;
run;


proc sort data=analysis;
by delivery_site_id;
run;

/********************************************************
/*C.Among Patients due 12 months before obs end, */
*********************************************************/;
data analysis2;
set analysis;
where one_year_due^=. and observation_end>=one_year_due;
run;
proc freq data=analysis2;
tables order_1y_corrected*documented_pap_1yr;
run;

%let SDH=SDH_Food; 
%let SDH=SDH_Housing;
%Let SDH=SDH_Trans;

/*	C1.Receipt of any screening order within 12 months*/

	proc genmod data=analysis2 descending;
	class &class &sdh /ref=first;
	model documented_pap_1yr=&sdh &covariates/link=log d=binomial type3;
	repeated subject=delivery_site_id/type=exch;
	LSmeans &sdh/ilink CL diff OM;
   	ods output LSmeans=order_&sdh lsmestimates=order_diffs_&sdh;
	lsmestimate &sdh '1 vs 2' -1 0 1/cl ilink OM e;
	lsmestimate &sdh '3 vs 2' -1 1 0/cl ilink OM e;
	run;

	proc sort data=order_&sdh out=var4_&sdh (keep=&sdh Mu LowerMu UpperMu);
	by &sdh;
	run;
	data var4_&sdh(keep=Outcome &sdh prob_CI);
	set var4_&sdh;
	length Outcome $30;
	Outcome="Documented Order within 1 year of due";
	format mu2 lower2 upper2 4.1;
	mu2=round(MU*100,0.1);
	mu_txt=put(mu2,4.1); 
	lower2=round(lowerMU*100,0.1);
	lower_txt=put(lower2,4.1);
	upper2=round(upperMU*100,0.1);
	upper_txt=put(upper2,4.1);
	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;

	proc sort data=order_diffs_&sdh out=diff4_&sdh (keep= Probz  label Mu LowerMu UpperMu);
	by label;
	run;

	data diff4_&sdh._b /*(keep=Outcome &sdh reference RR_CI probz)*/;
	set diff4_&sdh;
	length Outcome $30 &sdh reference $20;
	format mu2 lower2 upper2 4.2;
	Outcome="Documented Order within 1 year of due";
	reference='1_Positive Screen';
	if label='1 vs 2' then &SDH= '1_Positive Screen';
	if label='3 vs 2' then &SDH= '3_Never Screened';
	mu2=round(mu,0.01);
	mu_txt=put(mu2,4.2); 
	lower2=round(lowerMu,0.01);
	lower_txt=put(lower2,4.2);
	upper2=round(upperMu,0.01);
	upper_txt=put(upper2,4.2);
	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;


	data var4_&sdh;
	merge var4_&sdh diff4_&sdh._b;
	by outcome &sdh;
	run;
	proc print data=var4_&sdh; run;

	proc datasets library=work;
	delete order_diffs_&sdh diff4_&sdh  order_&sdh;
	quit;
/******************************************
/*D.Among patients with order, */
/******************************************/;
	data analysis3;
	set analysis2;
	where documented_pap_1yr=1;
	run;

/*	D2. receipt of any screen*/
	proc freq data=analysis3;
	tables &sdh;
	run;
	proc freq data=analysis3;
tables (race_ethnic2 language_c  FPL_cat2 age_cat  visit_cat)*payor_type_research;
run;


	proc genmod data=analysis3 descending;
	class &class &sdh /ref=first;
	/*FPL_cat2 omitted from transportationmodel to allow conergence*/
	model completed_pap_1yr=&sdh race_ethnic2 language_c  /*FPL_cat2*/ age_cat payor_type_research visit_cat/link=log d=binomial type3;repeated subject=delivery_site_id/type=exch;
	LSmeans &sdh/ilink CL OM;
	ods output LSmeans=screen_&sdh lsmestimates=screen_diffs_&sdh;
  	ods output LSmeans=order_&sdh lsmestimates=order_diffs_&sdh;
	lsmestimate &sdh '1 vs 2' -1 0 1/cl ilink OM e;
	lsmestimate &sdh '3 vs 2' -1 1 0/cl ilink OM e;
	run;

	proc sort data=order_&sdh out=var4_&sdh (keep=&sdh Mu LowerMu UpperMu);
	by &sdh;
	run;
	data var4_&sdh(keep=Outcome &sdh prob_CI);
	set var4_&sdh;
	length Outcome $30;
	Outcome="Completed within 1 year of due";
	format mu2 lower2 upper2 4.1;
	mu2=round(MU*100,0.1);
	mu_txt=put(mu2,4.1); 
	lower2=round(lowerMU*100,0.1);
	lower_txt=put(lower2,4.1);
	upper2=round(upperMU*100,0.1);
	upper_txt=put(upper2,4.1);
	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;

	proc sort data=order_diffs_&sdh out=diff4_&sdh (keep= Probz  label Mu LowerMu UpperMu);
	by label;
	run;

	data diff4_&sdh._b /*(keep=Outcome &sdh reference RR_CI probz)*/;
	set diff4_&sdh;
	length Outcome $30 &sdh reference $20;
	format mu2 lower2 upper2 4.2;
	Outcome="Completed within 1 year of due";
	reference='1_Positive Screen';
	if label='1 vs 2' then &SDH= '1_Positive Screen';
	if label='3 vs 2' then &SDH= '3_Never Screened';
	mu2=round(mu,0.01);
	mu_txt=put(mu2,4.2); 
	lower2=round(lowerMu,0.01);
	lower_txt=put(lower2,4.2);
	upper2=round(upperMu,0.01);
	upper_txt=put(upper2,4.2);
	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;


	data var4_&sdh;
	merge var4_&sdh diff4_&sdh._b;
	by outcome &sdh;
	run;
	proc print data=var4_&sdh; run;

	proc datasets library=work;
	delete order_diffs_&sdh diff4_&sdh  order_&sdh;
	quit;

